const api = require('../../utils/api.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: '',
    couponList: [],
    page: 1,
    couponDiscount: '', //优惠券折扣
    total: '', // 优惠券张
    end: false,
    payAmount: '', // 订单总金额
    activeIndex: '0'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userId: options.userId,
      payAmount: options.payAmount || '',
      couponList: []

    })
    this.getCouponList('0');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var scroll_page = this.data.page;
    if (!this.data.end) {
      scroll_page++;
      this.setData({
        page: scroll_page
      })
      this.getCouponList(this.data.activeIndex, scroll_page, )
    } else {
      return wx.showToast({
        title: '没有更多优惠券了',
        icon: 'none',
        duration: 1500
    })
      
    }
  },

   /* 获取优惠券 */
   getCouponList: function (status, page) {
    wx.stopPullDownRefresh()
    var that = this;
    var lists = this.data.couponList;
    var type = 0 + status;
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function () {
      api.post('funds/user/voucher/list', {
        page: page || 1,
        pageSize: 10,
        userId: that.data.userId || wx.getStorageSync('user').userId+'',
        status: type,
        shopId: app.globalData.shopId + '',
        payAmount: that.data.payAmount*100
      }).then(res => {
        if (res.data.success && res.data.data) {
          res.data.data.list.map(item => {
            var coupon_item = {};
            coupon_item.title = item.name;
            coupon_item.date = item.limitDay;
            coupon_item.count = parseInt(item.discount*1000)/100;
            coupon_item.status = status;
            coupon_item.limitUsed = item.limitTime;
            coupon_item.id = item.id;
            coupon_item.disType = item.disType;
            coupon_item.unUsableMsg =item.unUsableMsg
            if(typeof(item.maxAmount)==="number"){
              coupon_item.maxAmount = item.maxAmount/100;
            }
            coupon_item.minAmount = item.minAmount/100;
            lists.push(coupon_item)
          })
          that.setData({
            couponList: lists,
            total:  res.data.data.total
          })
          wx.hideLoading()
        }

        if(that.data.page*10 >= res.data.data.total){
          that.setData({
            end: true
          })
        }
        if(res.data.data.total==='0'){
          return wx.showToast({
            title: '暂无记录',
            icon: 'none',
            duration: 1500
        })
        }
      })

    }, 500)

  },
  /* 不使用优惠券 */
  clearCoupon:function(){
    wx.setStorageSync('couponDiscount', '');
    wx.navigateBack();
  },
  /* 保存按钮 */
  gotoPay: function() {
    wx.navigateBack();
  },
   /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      couponList: [],
      page: 1,
      end: false
    })
    this.getCouponList(this.data.activeIndex, 1);
  },
})